package com.tongji.admin.view
{
	import com.tongji.admin.event.GoodsAdminEvent;
	import com.tongji.admin.model.GoodsModel;
	import com.tongji.admin.view.popup.AddPictureTitleWin;
	import com.tongji.admin.vo.Brand;
	import com.tongji.admin.vo.Color;
	import com.tongji.admin.vo.Goods;
	import com.tongji.admin.vo.GoodsRelated;
	import com.tongji.admin.vo.Model;
	import com.tongji.admin.vo.Picture;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/7/
	 */ 
	public class GoodsBaseMediator extends Mediator
	{
		[Inject]
		public var view:GoodsBaseView;
		
		[Inject]
		public var model:GoodsModel;
		
		private var colorArray:ArrayCollection = new ArrayCollection();
		private var modelArray:ArrayCollection = new ArrayCollection();
		private var relateArray:ArrayCollection = new ArrayCollection();
		
		override public function onRegister():void
		{
			view.addPictureBtn.addEventListener(MouseEvent.CLICK, popupHandler);
			/*view.goodsBtnGroup.addBtn.addEventListener(MouseEvent.CLICK, addGoodsHandler);*/
			//view.addRelateBtn.addEventListener(MouseEvent.CLICK, addSizeHandler);
		}
		
		private function popupHandler(event:MouseEvent):void
		{
			var addPicTitleWin:AddPictureTitleWin = new AddPictureTitleWin();
			PopUpManager.addPopUp(addPicTitleWin, view, true);
			PopUpManager.centerPopUp(addPicTitleWin);
			mediatorMap.createMediator(addPicTitleWin);
		}
		
		private function addSizeHandler(event:MouseEvent):void
		{
			var color:Color = new Color;
			color.colorCreateDate = new Date();
			color.colorCreator = "caddy";
			color.colorName = "";
			color.colorNum = view.color.selectedColor;
			color.isDefault = false;
			colorArray.addItem(color);
			
			var model:Model = new Model();
			model.height = Number(view.heightTX.text);
			model.modelCreateDate = new Date();
			model.width = Number(view.widthTX.text);
			model.isDefault = false;
			model.modelSrc = "";
			model.packageSize = "";
			model.packageWeight = new Number();
			model.length = Number(view.lengthTX.text);
			model.modelCreator = "caddy";
			modelArray.addItem(model);
			
			/*var goodsRelate:GoodsRelated = new GoodsRelated();
			goodsRelate.goodsCount = 1;
			goodsRelate.goodsPrice = Number(view.unitPriceTX.text);
			goodsRelate.goodsPriceCreator = "caddy";
			goodsRelate.goodsPriceDate = new Date();
			goodsRelate.mapPicSrc = "";
			goodsRelate.model = model;
			goodsRelate.color = color;
		    relateArray.addItem(goodsRelate);*/
		}
		
		private function addGoodsHandler(event:MouseEvent):void
		{
			(colorArray[0] as Color).isDefault = true;
			(modelArray[0] as Model).isDefault = true;
			var goods:Goods = new Goods();
			goods.typeID = model.currentTypeID;
			goods.colors = colorArray;
			goods.models = modelArray;
			
			if(view.billBtnA.selected)
				goods.goodsBill = true;
			else
				goods.goodsBill = false;
			goods.goodsCityService = view.goodsCityService.selectedItem;
			goods.goodsCreateDate = new Date();
			goods.goodsCreator = "caddy";
			if(view.guaranteeBtnA.selected)
				goods.goodsGuaranteePeriod = Number(view.goodsGuaranteePeriod.text);
			else
				goods.goodsGuaranteePeriod = 0;
			goods.brandID = (view.brandList.selectedItem as Brand).brandID;
			goods.goodsInstallCost = Number(view.goodsInstallCostTX.text);
			goods.goodsLocation = view.goodsLocationTX.text;
			goods.goodsMaterial = view.goodsMaterialTX.text;
			goods.goodsName = view.goodsNameTX.text;
			goods.goodsPackage = view.goodsPackage.text;
			goods.goodsStyle = view.goodsStyleTX.text;
			goods.goodsType = view.goodsTypeTX.text;
			goods.goodsUnit = view.goodsUnitTX.text;
			goods.packageCount = int(view.packageCount.text);
			goods.goodsInstallCost = Number(view.goodsInstallCostTX.text);
			goods.goodsCityService = (view.goodsCityService.selectedItem as Object).name;
			
			var showPic:Picture = new Picture();
			showPic.picCreateDate = new Date();
			showPic.picCreator = "caddy";
			
			showPic.pictureType = 0;
			
			var showPic1:Picture = new Picture();
			showPic1.picCreateDate = new Date();
			showPic1.picCreator = "caddy";
			
			showPic1.pictureType = 1;
			
			var showPic2:Picture = new Picture();
			showPic2.picCreateDate = new Date();
			showPic2.picCreator = "caddy";
			
			showPic2.pictureType = 2;
			
			var showPic3:Picture = new Picture();
			showPic3.picCreateDate = new Date();
			showPic3.picCreator = "caddy";
			
			showPic3.pictureType = 2;
			
			var showPic4:Picture = new Picture();
			showPic4.picCreateDate = new Date();
			showPic4.picCreator = "caddy";
			
			showPic4.pictureType = 2;
			
			var showPic5:Picture = new Picture();
			showPic5.picCreateDate = new Date();
			showPic5.picCreator = "caddy";
			
			showPic5.pictureType = 2;
			
			goods.pictures = new ArrayCollection();
			goods.pictures.addItem(showPic);
			goods.pictures.addItem(showPic1);
			goods.pictures.addItem(showPic2);
			goods.pictures.addItem(showPic3);
			goods.pictures.addItem(showPic4);
			goods.pictures.addItem(showPic5);
			
			
			dispatch(new GoodsAdminEvent(GoodsAdminEvent.ADD_A_GOODS, goods));
		}
	}
}